﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TrailMap.TileSource
{
    public class YahooTileSource : Microsoft.Maps.MapControl.TileSource, IMapProvider
    {
        //http://maps1.yimg.com/hx/tl?b=1&v=4.3&t=h&.intl=en&x=14&y=5&z=7&r=1
        private const string hybridURL = @"http://maps{0}.yimg.com/hx/tl?v=4.3&t=h&.intl=en&x={1}&y={2}&z={3}&r=1";
        //http://maps3.yimg.com/ae/ximg?v=1.9&t=a&s=256&.intl=en&x=15&y=7&z=7&r=1
        private const string sateliteURL = @"http://maps{0}.yimg.com/ae/ximg?v=1.9&t=a&s=256&.intl=en&x={1}&y={2}&z={3}&r=1";
        //http://maps1.yimg.com/hx/tl?b=1&v=4.3&.intl=en&x=12&y=7&z=7&r=1
        private const string normalURL = @"http://maps{0}.yimg.com/hx/tl?v=4.3&.intl=en&x={1}&y={2}&z={3}&r=1";

        private MapType _MapMode = MapType.Normal;

        //Constructor Called by XAML instanciation; Wait for MapMode to be set to initialize services
        public YahooTileSource()
            : base()
        {
        }

        public Microsoft.Maps.MapControl.TileSource HybridBase
        {
            get
            {
                YahooTileSource retVal = new YahooTileSource();
                retVal.MapMode = MapType.Satellite;
                return retVal;
            }
        }

        public string FileExtn
        {
            get
            {
                if (this.MapMode == MapType.Satellite)
                {
                    return "jpeg";
                }
                else
                {
                    return "png";
                }
            }
        }

        public MapType MapMode
        {
            get { return _MapMode; }
            set
            {
                _MapMode = value;
            }
        }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            string url = string.Empty;

            int serverNum = ((GetServerNum(x,y, 2)) + 1);
            int posY = (((1 << zoomLevel) >> 1) - 1 - y);
            int zoom = zoomLevel + 1;

            switch (_MapMode)
            {
                case MapType.Normal:
                    {
                        url = string.Format(normalURL, serverNum, x, posY, zoom);
                    }
                    break;
                case MapType.Satellite:
                    {
                        url = string.Format(sateliteURL,serverNum, x, posY, zoom);
                    }
                    break;
                case MapType.Hybrid:
                    {
                        url = string.Format(hybridURL, serverNum,x, posY, zoom);
                    }
                    break;
            }

            return new Uri(url);
        }
        /// <summary>
        /// gets server num based on position
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        internal int GetServerNum(int x, int y, int max)
        {
            return (x + 2 * y) % max;
        }
    }
}
